//package com.ks.servlet;
//
//import java.io.IOException;
//import java.net.URLDecoder;
//import java.util.UUID;
//
//import javax.servlet.ServletException;
//import javax.servlet.annotation.WebServlet;
//import javax.servlet.http.Cookie;
//import javax.servlet.http.HttpServlet;
//import javax.servlet.http.HttpServletRequest;
//import javax.servlet.http.HttpServletResponse;
//
//import org.apache.commons.logging.Log;
//import org.apache.commons.logging.LogFactory;
//
//import com.ks.entity.User;
//import com.ks.service.UserService;
//import com.ks.utils.Factory;
//
//@WebServlet("/SSOlogin")
//public class SSOServer extends HttpServlet {
//	private static Log log = LogFactory.getLog(LoginServlet.class);
//	private static final long serialVersionUID = 1L;
//	UserService userServiceImpl = (UserService) Factory.getBean("userServiceImpl");
//
//	@Override
//	protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
//		// 获得原始请求的URL并保存传递，登录成功时，让浏览器再次跳转到该URL
//		String origURL = req.getParameter("origURL");
//		req.setAttribute("origURL", origURL);// 目的在于在整个会话中，把原始的请求地址保存到req中
//		req.getRequestDispatcher("WEB-INFO/view/login.jsp").forward(req, resp);
//	}
//
//	@Override
//	protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
//		String account = req.getParameter("account");
//		String password = req.getParameter("password");
//		String origURL = req.getParameter("origURL");
//
//		// 按照account查找用户
//		User user = null;
//		try {
//			user = userServiceImpl.getUserById(account);
//			if (user != null) {
//				if (user.getPassword() == password)// .equals(password)
//				{
//					// 1、生成token java UUID随机生成
//					String token = UUID.randomUUID().toString().replace("-", "").toLowerCase();
//					// 2、将token存到全局唯一数据结构中
////					TokenUserData.addToken(token, user);
//					// 3、写入cookie
//					Cookie tokenCookie = new Cookie("token", token);
//					tokenCookie.setPath("/");// localhost:8091 同一个域，不同路径；
//					tokenCookie.setHttpOnly(true);
//					resp.addCookie(tokenCookie);
//					// 4、跳转请求
//					if (origURL != null) {
//						origURL = "login_success";
//					} else {
//						origURL = URLDecoder.decode(origURL, "utf-8");
//					}
//					resp.sendRedirect(origURL);
//				} else {
//					backToLoginPage(req, resp, account, origURL, "密码不正确");
//				}
//			} else {
//				this.backToLoginPage(req, resp, account, origURL, "用户不存在");
//			}
//		} catch (Exception e) {
//			e.printStackTrace();
//			backToLoginPage(req, resp, account, origURL, "系统错误");
//
//		}
//
//	}
//
//	private void backToLoginPage(HttpServletRequest req, HttpServletResponse resp, String account, String origURL,
//			String errInfo) throws ServletException, IOException {
//		req.setAttribute("origURL", origURL);
//		req.setAttribute("account", account);
//		req.setAttribute("errInfo", errInfo);
//		req.getRequestDispatcher("/WEB-INF/view/login.jsp").forward(req, resp);
//
//	}
//}
